// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import AudioMgr from "./common/AudioMgr";
import RankDialog from "./dialog/RankDialog";
import WXRankUtil from "./utils/WXRankUtil";

const { ccclass, property } = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    _root_view: fgui.GComponent;

    _integral_view: fgui.GLabel;

    win: Window;

    // use this for initialization
    onLoad() {
        fgui.addLoadHandler();

        fgui.GRoot.create();

        fgui.UIPackage.loadPackage("UI/IMGame", this.onUILoaded.bind(this));
    }

    onUILoaded() {
        // fgui.UIPackage.addPackage("UI/IMGame");

        this._root_view = fgui.UIPackage.createObject("IMGame", "StartPanel").asCom;

        this._root_view.makeFullScreen();

        this.loadRootUI();

        //获取数据
        if (cc.sys.localStorage.getItem('bgVoice') === null) {
            //没有设置过

            //保存数据 100 播放  101 不播放
            // cc.sys.localStorage.setItem('bgVoice', "100");

            gg.audioMgr.playBGM("piano");
        } else {
            //有设置

            if (cc.sys.localStorage.getItem('bgVoice') === "100") {
                gg.audioMgr.playBGM("piano");
            } else {
                gg.audioMgr.stopBGM();
            }
        }

    }

    loadRootUI() {
        //积分
        this._integral_view = this._root_view.getChild("n16").asLabel;

        this._root_view.getChild("n7").onClick(this.onSetClick, this);
        this._root_view.getChild("n8").onClick(this.onPlayClick, this);

        this._root_view.getChild("n20").onClick(this.onPropClick, this);
        this._root_view.getChild("n21").onClick(this.onStickClick, this);
        this._root_view.getChild("n22").onClick(this.onRankClick, this);
        this._root_view.getChild("n23").onClick(this.onGigtClick, this);
        // _root_view.getChild("n19").onClick(this.onStickClick, this);

        fgui.GRoot.inst.addChild(this._root_view);

    }


    // called every frame
    update(dt) {

    }


    //设置按钮点击
    onSetClick() {
        console.log("hello world");
        // _root_view.Dispose();
        // cc.director.loadScene("resurrection");
        // var num = _integral_view.text;
        // _integral_view.text = ++num + "";
        // this.showDialog();

        // gg.rankDialog.showEx(",请稍后再试!", 2);
        // this.dialog =new RankDialog();
        // this.dialog.showEx("",1);

        // gg.audioMgr.stopBGM();

        cc.director.loadScene("setting");

    }


    showDialog() {
        // win = new Window();
        // win.contentPane = fgui.UIPackage.createObject("IMGame", "RankDialog").asCom;
        // win.Show();
    }


    //开始按钮点击
    onPlayClick() {
        //加载游戏场景
        cc.director.loadScene("game");
    }

    //道具按钮点击
    onPropClick() {
        cc.director.loadScene("resurrection");
    }

    //箭按钮点击
    onStickClick() {
        WXRankUtil.submitScoreButtonFunc(Math.floor(Math.random() * 1000));
    }

    //排行按钮点击
    onRankClick() {
        cc.director.loadScene("mainRank");
    }

    //礼物按钮点击
    onGigtClick() {

    }
}